package operator

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/pkg/mildware"
	"project/pkg/models"
	"project/pkg/validate"
)

func (h OHandler) Create(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.UpdateInfo)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Op:Create with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Op:Create with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//token
	ID, _, name, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("Op:UpdateInfo mildware.GetCyrrentUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	demo := &models.OperationInfo{
		CreateId:    ID,
		CreateName:  name,
		Username:    req.Username,
		Password:    req.Password,
		StudentName: req.StudentName,
	}
	//保存
	err = h.M.OpUpdateInfoBuID(demo)
	if err != nil {
		zap.L().Error("Op:UpdateInfo h.M.OpUpdateInfoBuID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}
